package cn.edu.scnu.lastandroid.ui.place

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import android.widget.ImageView
import android.widget.Toast
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.edu.scnu.lastandroid.MainActivity
import cn.edu.scnu.lastandroid.R
import cn.edu.scnu.lastandroid.ui.weather.WeatherActivity

class PlaceFragment: Fragment() {

    val viewModel by lazy { ViewModelProvider(this).get(PlaceViewModel::class.java) }

    private lateinit var adapter: PlaceAdapter

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_place, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        if (viewModel.isPlaceSaved()) {
            val place=viewModel.getSavedPlace()
            val intent=Intent(context,WeatherActivity::class.java).apply {
                putExtra("location_lng",place.lng)
                putExtra("location_lat",place.lat)
                putExtra("place_name",place.name)
            }
            startActivity(intent)
            activity?.finish()
            return
        }
        val layoutManager = LinearLayoutManager(activity)
        val recyclerView : RecyclerView? = view?.findViewById(R.id.recyclerView)
        recyclerView?.layoutManager = layoutManager
        /*下面这个if语句属于 15.5最后一部分代码，这里fozu帮忙写了，为了15.6方便改动，bug部分没de，如有问题概不负责*/
        if (activity is MainActivity &&viewModel.isPlaceSaved()){
            val place =viewModel.getSavedPlace()
            val intent = Intent(context, WeatherActivity::class.java).apply{
                putExtra("location_lng",place.lng)
                putExtra("location_lat",place.lat)
                putExtra("place_name",place.name)
            }
            startActivity(intent)
            activity?.finish()
            return
        }
        adapter = PlaceAdapter(this, viewModel.placeList)
        recyclerView?.adapter = adapter
        val searchPlaceEdit : EditText? = view?.findViewById(R.id.searchPlaceEdit)
        searchPlaceEdit?.addTextChangedListener{  editable ->
            val content = editable.toString()
            if (content.isNotEmpty()){
                viewModel.searchPlaces(content)
            } else {
                recyclerView?.visibility = View.GONE
                val bgImageView : ImageView? = view?.findViewById(R.id.bgImageView)
                bgImageView?.visibility = View.VISIBLE
                viewModel.placeList.clear()
                adapter.notifyDataSetChanged()
            }
        }
        viewModel.placeLiveData.observe(viewLifecycleOwner){result ->
            val places = result.getOrNull()
            if (places != null){
                recyclerView?.visibility = View.VISIBLE
                val bgImageView : ImageView? = view?.findViewById(R.id.bgImageView)
                bgImageView?.visibility =View.GONE
                viewModel.placeList.clear()
                viewModel.placeList.addAll(places)
                adapter.notifyDataSetChanged()
            } else{
                Toast.makeText(activity, "未能查询到任何地点", Toast.LENGTH_SHORT).show()
                result.exceptionOrNull()?.printStackTrace()
            }
        }
    }

}